<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en" class="no-js">
<!--<![endif]-->

<head>
<meta charset="utf-8">
<title>买盘生成订单</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="" name="description">
<meta content="" name="author">
<script>BASE_URL ='${request.getContextPath()}'</script>
<link href="${request.getContextPath()}/Public/assets/admin/css/css.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/plugins/simple-line-icons/simple-line-icons.min.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/plugins/uniform/css/uniform.default.css" rel="stylesheet" type="text/css">

<link href="${request.getContextPath()}/Public/assets/global/css/components-rounded.css" id="style_components" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/css/plugins.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/admin/css/layout.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/admin/css/themes/default.css" rel="stylesheet" type="text/css" id="style_color">
<link href="${request.getContextPath()}/Public/assets/admin/css/custom.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/global/plugins/bootstrap-datepicker/css/bootstrap-datepicker.min.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/assets/admin/css/common.css" rel="stylesheet" type="text/css">
<link href="${request.getContextPath()}/Public/css/customerDetail.css" rel="stylesheet" type="text/css">


<link rel="shortcut icon" href="${request.getContextPath()}/Public/favicon.ico">
<!--[if lt IE 9]>
<script src="${request.getContextPath()}/Public/assets/global/plugins/respond.min.js"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/excanvas.min.js"></script>
<![endif]-->
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery-migrate.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery-ui/jquery-ui.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/bootstrap-hover-dropdown/bootstrap-hover-dropdown.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery-slimscroll/jquery.slimscroll.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery.blockui.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery.cokie.min.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/global/plugins/uniform/jquery.uniform.min.js" type="text/javascript"></script>

<script src="${request.getContextPath()}/Public/assets/global/plugins/jquery.sparkline.min.js" type="text/javascript"></script>

<script src="${request.getContextPath()}/Public/assets/global/plugins/bootstrap-datepicker/js/bootstrap-datepicker.js" type="text/javascript"></script>

<script src="${request.getContextPath()}/Public/assets/global/scripts/metronic.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/admin/scripts/layout.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/admin/scripts/indexComponent.js" type="text/javascript"></script>
<script src="${request.getContextPath()}/Public/assets/admin/scripts/commonUtils.js" type="text/javascript"></script>

<!-- select2控件 -->
<!-- select2控件 -->
<script type="text/javascript" src="${request.getContextPath()}/Public/assets/global/plugins/select2/select2.js"></script>
<script type="text/javascript" src="${request.getContextPath()}/Public/assets/global/plugins/select2/select2_locale_zh-CN.js"></script>
<link href="${request.getContextPath()}/Public/assets/global/plugins/select2/select2.css" rel="stylesheet" type="text/css">

<script type="text/javascript" src="${request.getContextPath()}/Public/My97DatePicker/WdatePicker.js"></script>
	<!-- 数字小数点后两位工具类 -->
<script type="text/javascript" src="${request.getContextPath()}/Public/js/common/util.js"></script>
</head>
<body class="page-header-top-fixed">
<div class="page-header">
	<!--头部开始-->
	<#include "/include/top.html" />
	<!-- 头部信息结束 -->

	<!-- 头部菜单开始 -->
	<#include "/include/top_menu.html" />
	<!-- 头部菜单结束 -->
</div>
<!-- END HEADER -->
<div class="page-container">
	<div class="page-content">
		<div class="container-fluid">
			<div class="row margin-top-10">
				<div class="col-md-12">
					<div class="portlet light bordered" align="center">
						<form action=""  method="post" class="form-horizontal" id="buyfrom">
							<input type="hidden" id="poolId" name="poolId" value="${poolId}">
							<input type="hidden" id="poolType" name="poolType" value="${poolType}">
						 <table id="myTable" class="table table-striped table-bordered table-advance table-hover ttTbl">
						          	<thead>
										<tr>
											<th>产品名称</th>
											<#if attrs??>
												<#list attrs as attr>
												<th>
													${attr.name}
													<input type="hidden" name="attrNames" value="${attr.name}">
													<input type="hidden" name="attrIds" value="${attr.id?c}">
												</th>
												</#list>
											</#if>
											<th>计量单位</th>
											<th>数量</th>
											<th>单位/元</th>
											<th>总金额/元</th>
										</tr>
								</thead>
								<tbody>
						      		<tr id="more">
									<td><select name="productid"  style="width: 180px; height: 28px;">
										<#list prolist as pro>
											<option value="${pro.id?c}" <#if proId??&&proId==pro.id>selected</#if>>${pro.name} </option>
										</#list>
									</select>
									</td>
									
									<#if attrs??>
												<#list attrs as attr>
													<#if attr.fillmode==0>
														<td>
														<#if (attrValuesList?size>0)>
														<#assign flag=true/>
														<#list attrValuesList as attrvals>
															 <#if attr.name == attrvals.attrName >
															 <#assign flag=false/>
																<input type="text" name="attrValues${attr.id?c}"  value="${attrvals.attrValue}"/>														
															 </#if>
														</#list>
														<#if  flag>
															<input type="text" name="attrValues${attr.id?c}"  value=""/>
														</#if>
														<#else>
														<input type="text" name="attrValues${attr.id?c}"  value=""/>
														</#if>
														<#if attr.mainproperty??&&attr.mainproperty==1>
														<font color="red">*(必填项)*</font>
														</#if>
														</td>
													</#if >
													<#if attr.fillmode==1||attr.fillmode==3>
														<td>
														<#if (attrValuesList?size>0)>
														<#assign flag=true/>
														<#list attrValuesList as attrvals>
															 <#if attr.name == attrvals.attrName >
															 	<#assign flag=false/>
															<select name="attrValues${attr.id?c}">
																<option value="">---请选择---</option>
															<#list attr.optionsValue as va>
																<option value="${va}" <#if va==attrvals.attrValue>selected</#if>>${va}</option>
															</#list>	
															</select>	
														</#if>
														</#list>
														<#if  flag>
															<select name="attrValues${attr.id?c}">
																<option value="">---请选择---</option>
															<#list attr.optionsValue as va>
																<option value="${va}" >${va}</option>
															</#list>	
															</select>
														</#if>	
														<#else>
														<select name="attrValues${attr.id?c}">
																<option value="">---请选择---</option>
															<#list attr.optionsValue as va>
																<option value="${va}" >${va}</option>
															</#list>	
															</select>
															</#if>	
															<#if attr.mainproperty??&&attr.mainproperty==1>
																<font color="red">*(必填项)*</font>
															</#if>												
														</td>
													</#if >
										</#list>
									</#if>
									
									<td>
										<select name="unit">
											<option value="0"  <#if buypool?? && buypool.unit?? && buypool.unit=="0">selected</#if>>吨</option>
											<option value="1"  <#if buypool?? && buypool.unit?? && buypool.unit=="1">selected</#if>>千克</option>
										</select>
									</td>
									<td><input type="text" name="quantity" id="quantity0"  readonly="readonly" <#if buypool.quantit??> value="${buypool.quantit}"</#if> onblur="compute(0);"/></td>
									<td><input type="text" name="price" id="price0" onblur="compute(0);" <#if buypool.lastPrice??> value="${buypool.lastPrice}"</#if>/></td>
									<td><input type="text" id="count0" value=""  readonly="readonly"/></td>
									</tr>
								  </tbody>
								</table>
								<!-- <p align="left"><input type="button"   onclick="addMore()" name=""  value="+"/>
								 <input type="button"  onclick="delMore(this)" name=""  value="-"/></p> -->
								<div>
								<input type="hidden" name="sourceId" value="${sourceId}">
								<input type="hidden" name="source" value="${source}">
								<div class="form-group">
						   			 <label  class="col-sm-2 control-label">买方企业：</label>
						    		<div class="col-sm-2">
						    		<input type="hidden" id="buyId" name="buyId"  value="${buypool.customerId?c}">
						       		 <input class="form-control"  id="companyid"  value="${buypool.companyname}" readonly="readonly"/>
						    		</div>
						    		<label  class="col-sm-2 control-label">卖方企业：</label>
						    		<div class="col-sm-2">
						    		<input type="hidden" id="sellId" name="sellId"  value="${buypool.lastCustomerId?c}">
						       		<input  class="form-control"  id="companyid2"  value="${buypool.lastCustomerName}" readonly="readonly"/>
						    		</div>
						 		</div>
							  <div class="form-group">
						    		<label  class="col-sm-2 control-label">交货日期：</label>
						    		<div class="col-sm-2">
						       		<input type="text" class="form-control" id="deliveryDate" name="deliveryDate" onClick="WdatePicker()" readonly="readonly">
						    		</div>
						    		
						    		<label  class="col-sm-2 control-label">交货仓库：</label>
						    		<div align="left">
						    		&nbsp;
						    		<select id="wareProvince" name="wareProvince" style="width: 100px;height: 34px;" onchange="selWareName(this.value);">
								    		<option value="-1">请选择省份</option>
								    	<#list arealist as area>
								    			<option value="${area.id}">${area.name}</option>
								    	</#list>
						    		</select>
						    		<select name="wareId" id="wareId" style="height: 34px;">
						    			<option value="-1">请选择仓库</option>
						    		</select>
						    		</div>
						 	  </div>
						 	  <div class="form-group">
						   			 <label  class="col-sm-2 control-label">付款方式：</label>
						    		<div class="col-sm-2">
						       		<select name="receipttype"  style="width: 148px;height: 34px;">
							       		<option value="1">款到发货</option>
							       		<option value="2">货到付款</option>
						       		</select>
						    		</div>
						    		<label  class="col-sm-2 control-label">磅差：</label>
						    		<div class="col-sm-2">
						       		<input type="text"  id="poundDiff" name="poundDiff">‰
						    		</div>
						 	  </div>
							<div class="form-group">
						   			 <label  class="col-sm-2 control-label">结算方式：</label>
						    		<div class="col-sm-2">
						       		<select id="payment" name="payment" style="width: 148px;height: 34px;">
						       		<option value="1">现款现汇</option>
						       		<option value="2">银行承兑</option>
						       		<option value="3">安全支付</option>
						       		</select>
						    		</div>
						    		<label  class="col-sm-2 control-label">交货方式：</label>
						    		<div class="col-sm-2">
							       		<select name="delivery" style="width: 148px;height: 34px;">
							       		<option value="1"  >卖方送货</option>
							       		<option value="2"  >买方自提</option>
						       		</select>
						 		   </div>
						    		</div>
							</div>
							<br><br>
							<input type="hidden" name="confirmStatus" id="confirmStatus">
						  <div class="form-group">
						    <div class="col-sm-offset-2 col-sm-10">
						    	<button id="Button"  onclick="javascript :history.back(-1);" type="button" class="btn green pull-left">
									返回 <i class="m-icon-swapright m-icon-white"></i>
								</button>
								<button id="cButton"  onclick="copyURL();" type="button" class="btn green pull-left">
									复制订单网址 <i class="m-icon-swapright m-icon-white"></i>
								</button>
								<button id="SaveOrContent"   type="button" class="btn green pull-left">
									提交并预览合同 <i class="m-icon-swapright m-icon-white"></i>
								</button>
						      	<button type="button"  id="savetButton" class="btn green pull-left">
									提交，不生成合同<i class="m-icon-swapright m-icon-white"></i>
								</button>
						    </div>
						  </div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<!-- 底部开始 -->
<#include "/include/foot.html" />
<!-- 底部结束 -->
</body>
<script>
var paymentType=-1; 
jQuery(document).ready(function() {
	   Metronic.init(); // init metronic core componets
	   Layout.init(); // init layout
	   compute(0);
	   checkNum($('#price0'));
	});
	recordDate();
	//初始化日期
	function recordDate() {
		var nowDate = new Date();
        //$('#date-picker').attr("date-date",nowDate.getFullYear()+"/"+parseInt(nowDate.getMonth()+1)+"/"+nowDate.getDate());
        $('.date-picker').datepicker({
    	    autoclose: true	   
    	});
        
        
	}
	
	function selWareName(value){
		if(value==-1){
			var ops =  document.getElementById("wareId").options;
			ops.length=0;
			ops.add(new Option("请选择仓库","-1"));
			return;
		}
		$.ajax({
			url : "${request.getContextPath()}/buyOrder/selWarehouseByArea.do",
			type : "post",
			dataType:"json",
			async: false,
			data :{"areaId":value,"companyName":$("#companyid2").val()},
			success : function(result) {
				var ops =  document.getElementById("wareId").options;
				ops.length=0;
				if(result["list"].length>0){
					for(var i=0;i<result["list"].length;i++){
						ops.add(new Option(result["list"][i]["name"],result["list"][i]["id"]));					
					}
				}else{
					ops.add(new Option("请选择仓库","-1"));
				}
			}
		});
	}
	
	$("#savetButton").bind("click",function(){
		$("#confirmStatus").val("0");
		if(check()){
			$.ajax({
				url : "${request.getContextPath()}/buyOrder/createOrder.do",
				type : "post",
				dataType:"json",
				async: false,
				data : $("#buyfrom").serialize(),
				success : function(result) {
				   if(result.success!=null){
					   alert("提交成功！");
					   window.location.href="${request.getContextPath()}/order/ordersList.do";
					}else {
						alert(result.message);
			   		}
				   }
			});
		}
	});
	
	$("#SaveOrContent").bind("click",function(){
		$("#confirmStatus").val("1");
		if(check()){
			$.ajax({
				url : "${request.getContextPath()}/buyOrder/createOrder.do",
				type : "post",
				dataType:"json",
				async: false,
				data : $("#buyfrom").serialize(),
				success : function(result) {
				   if(result.success!=null){
					   	alert("提交成功！");
				      		window.location.href="${request.getContextPath()}/contract/generateContractByTemplate.do?orderId="+result.oid;
					}else {
						alert(result.message);
						window.location.href = "${request.getContextPath()}/business/index.do";
			   		}
				   }
			});
		}
	});

	
	function resultFormatResult(medata) {
        return medata.text; 
	} 
	      
	function resultFormatSelection(medata) {
	        return medata.text; 
	} 
	
	
	function quit(){
		
  		window.location.href="${request.getContextPath()}/buypool/buylist.do";

	}
	var  k =0;
	 function addMore() {
		 k++;

		 var str="<td><select name='productid'  style='width: 180px; height: 28px;'><#list prolist as pro><option value='${pro.id?c}' <#if proId??&&proId==pro.id>selected</#if>>${pro.name}</option></#list></select></td>";
		
		 var str2 ="<#if attrs??><#list attrs as attr><#if attr.fillmode==0><td><#if (attrValuesList?size>0)><#assign flag=true/><#list attrValuesList as attrvals> <#if attr.name == attrvals.attrName > <#assign flag=false/><input type='text' name='attrValues${attr.id?c}'  value='${attrvals.attrValue}'/> </#if></#list><#if  flag><input type='text' name='attrValues${attr.id?c}'  value=''/></#if><#else><input type='text' name='attrValues${attr.id?c}'  value=''/></#if><#if attr.mainproperty??&&attr.mainproperty==1><font color='red'>*(必填项)*</font></#if></td></#if >"
				+"<#if attr.fillmode==1||attr.fillmode==3><td><#if (attrValuesList?size>0)><#assign flag=true/><#list attrValuesList as attrvals> <#if attr.name == attrvals.attrName > <#assign flag=false/><select name='attrValues${attr.id?c}'><option value=''>---请选择---</option><#list attr.optionsValue as va><option value='${va}' <#if va==attrvals.attrValue>selected</#if>>${va}</option></#list></select></#if></#list><#if  flag><select name='attrValues${attr.id?c}'><option value=''>---请选择---</option>"
				+"<#list attr.optionsValue as va><option value='${va}' >${va}</option></#list></select></#if><#else><select name='attrValues${attr.id?c}'><option value=''>---请选择---</option><#list attr.optionsValue as va><option value='${va}' >${va}</option></#list></select></#if>  <#if attr.mainproperty??&&attr.mainproperty==1><font color='red'> *(必填项)*</font></#if></td></#if ></#list></#if>";
				
		 var  str3="<td><select name='unit'><option value='0' <#if buypool.unit??&&buypool.unit=='0'>selected</#if>>吨</option><option value='1'  <#if buypool.unit??&&buypool.unit=='1'>selected</#if>>千克</option></select></td>";
		 var  str4='<td><input type="text" name="quantity" id="quantity'+k+'" value="" onblur="compute('+k+');"></td>'; 
		 var  str5='<td><input type="text" name="price" id="price'+k+'" value="" onblur="compute('+k+');"></td>'; 
		 var  str6='<td><input type="text" id="count'+k+'" value="" readonly="readonly"></td>'; 
			
		 str="<tr>" + str+str2+str3+str4+str5+str6 +"</tr>";
		$("#myTable tbody").append(str); 
	}
	 
	 function delMore(obj){
		  
		 var tb = document.getElementById("myTable");
		 
		 var len = tb.rows.length;
		 if(len-1>1){
			 tb.deleteRow(len-1);
			 k--;
		 }else{
			 alert("至少有一个产品");
		 }
	 }
	
	 function compute(o){
		var quantity= document.getElementById("quantity"+o).value;
		 var price = document.getElementById("price"+o).value;
		 document.getElementById("count"+o).value =(quantity*price).toFixed(2);
		 
	 }
	 
	 function copyURL(){
			 var clipBoardContent=this.location.href;
			 //window.clipboardData.setData("Text",clipBoardContent);
			 //alert(clipBoardContent);
			 CommonUtils.succModal("",clipBoardContent);
	 }
	
	 function check(){
		 	var num =/^[0-9]+([.]{1}[0-9]+){0,1}$/;
		 	for(var i=0;i<k+1;i++){
			 	var quantity=document.getElementById("quantity"+i).value;
				 	if(!num.test(quantity)){
						alert("数量只能为整数和小数！");
						return false;
					}
				 	if(quantity==0){
				 		alert("数量不能为0！");
						return false;
				 	}
		 	}
		 	for(var i=0;i<k+1;i++){
			 	var price=document.getElementById("price"+i).value;		
				 	if(!num.test(price)){
						alert("单价只能为整数和小数！");
						return false;
					}
				 	if(price==0){
				 		alert("单价不能为0！");
						return false;
				 	}
		 	}
		 
			var sellId=document.getElementById("sellId").value;
			var buyId=document.getElementById("buyId").value;
			if($.trim(sellId) ==""){
				alert("请选择卖方企业！");
				return false;
			}
			if(sellId==buyId){
				alert("卖方企业不能和买方企业相同！");
				return false;
			}
			
			var deliveryDate=document.getElementById("deliveryDate").value;
			if($.trim(deliveryDate) ==""){
				alert("交货日期不能为空！");
				return false;
			}
			var wareProvince=document.getElementById("wareProvince").value;
			if($.trim(wareProvince) =="-1"){
				alert("请选择省份！");
				return false;
			}
			var wareId=document.getElementById("wareId").value;
			if($.trim(wareId) =="-1"){
				alert("请选择交货仓库！");
				return false;
			}
			var poundDiff=document.getElementById("poundDiff").value;
			if(!num.test(poundDiff)){
				alert("磅差只能为整数和小数！");
				return false;
			}
			if(poundDiff==0){
		 		alert("磅差不能为0！");
				return false;
		 	}
			
			var payment=$("#payment").val();
			 if(payment==3){				 
				paymentValidate(sellId);
				if(paymentType==0){
					alert("你没有有色网商城支付账号，无法进行安全支付！");
					return false;
				}
				paymentValidate(buyId);
				if(paymentType==0){
					alert("你没有有色网商城支付账号，无法进行安全支付！");
					return false;
				}
			 }
			
			return true;
		}
	 function paymentValidate(customerId){
		 
			$.ajax({
				url : "${request.getContextPath()}/dealMake/checkUserPay.do",
				type : "post",
				dataType:"json",
				async: false,
				data :{"customerId":customerId}, //联动查询的字符
				success : function(data) {
					if(data == 1){//没有  不可支付
						paymentType=0;
					}else{//可支付
						paymentType=1;
					}
				}
			});
		}
</script>
</html>

